<?php

// +----------------------------------------------------------------------
// | 权限组控制器
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 微尘 <yicmf@qq.com>
// +----------------------------------------------------------------------

namespace app\controller\pay\admin;

use app\model\pos\Pos as PosModel;
use app\model\ucenter\User as UserModel;
use yicmf\builder\Edit;
use yicmf\builder\Table;
use app\controller\admin\Common;
use app\event\file\Attachment as AttachmentEvent;
use app\model\ucenter\UserMoneyLog as UserMoneyLogModel;
use app\model\pay\PointProduct as PointProductModel;
use app\event\pay\PointProduct as PointProductEvent;

class PointProduct extends Common
{
    protected function initialize()
    {
        parent::initialize();
    }

    public function _filter()
    {
        $where[] = ['status', '>', -2];
        return $where;
    }

    public function index()
    {

        $statusOptions = [
            [
                'id' => 1,
                'value' => '未划拨'
            ],
            [
                'id' => 2,
                'value' => '已划拨'
            ]
        ];
        $where = $this->_filter();

        //ID 姓名 手机号 等级  提现金额 手续费 提现费 实际入账  审核状态 驳回原因 申请时间  审核时间 事项 时间 资金类型
        // 显示页面
        $builder = new Table();
        $builder->model(PointProductModel::class)
            ->where($where)
//                ->suggest('高等级可以对低等级用户进行操作或者授权')
//            ->searchSelect('is_bind', '划拨状态', $statusOptions)
//            ->searchText('sn_start', '机身开始号')
//            ->searchText('sn_end', '机身结束号')
//            ->searchText('result_count', '显示总数')
//            ->keyLeftLeader('checkbox')
            ->keyId()
            ->keyText('name', '银行名称')
            ->keyImage('logo', 'logo')
            ->keyText('chaxun', '查询方法')
            ->keyText('price', '价值（每万积分）')
            ->keyMap('flag', '类别', ['银行', '运营商'])
            ->keyText('commission', '佣金')
//            ->keyImage('poster_image', '海报')
            ->keyText('sort', '排序')
            ->keyCreateTime()
            ->actionUpdate('update?id={$id}')
            ->actionDelete();
        return $builder->fetch();
    }


    public function update()
    {
        if ($this->request->isPost()) {
            $event = new PointProductEvent();
            $data = $event->update($this->request->param(), $this->user);
            if (0 === $data['code']) {
                $this->_returnSuccess($data['message']);
            } else {
                $this->_returnError($data['message'], false);
            }
        } else {
            // 同步节点与菜单
            if ($this->request->param('id')) {
                $group_id = $this->request->param('id');
                $data = PointProductModel::where('id', $group_id)->find();
                if (!$data) {
                    $this->_returnError('记录不存在');
                }
            } else {
                $this->_returnError('记录不存在');
            }
            $data['status'] = 1;
            $builder = new Edit();
            $builder->title('修改')
                ->keyText('name', '名称')
                ->keyImage('logo', 'logo')
                ->keyText('label1', '标签1')
                ->keyText('label2', '标签2')
                ->keyTextArea('chaxun', '查询方法')
                ->keyDecimal('price', '价值', '（每万积分）')
                ->keyRadio('flag', '类别', ['银行', '运营商'])
                ->keyText('commission', '佣金')
                ->keyEditor('tips','温馨提示')
//            ->keyImage('poster_image', '海报')
                ->keySort('sort', '排序')
                ->data($data)
                ->buttonSubmit();
            return $builder->fetch();
        }
    }

    /**
     * 删除信息
     * 改变status信息删除.
     */
    public function delete()
    {
        $event = new PointProductEvent();
        $result = $event->delete($this->request->get(), $this->user);
        if (0 == $result['code']) {
            $this->_returnSuccess('删除成功');
        } else {
            $this->_returnError('失败，失败信息：' . $result['message']);
        }
    }


}
